<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="600"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="100px"
      label-position="right"
    >
      <sk-tabs type="border-card">
        <el-tab-pane ref="tab" label="点棍信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="form.code" placeholder="自动生成" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分组" prop="category_id">
                <skDicSelect
                  v-model="this.form.category_id"
                  :code="'gluecategory'"
                ></skDicSelect>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规格" prop="spec">
                <el-input v-model="form.spec" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="代号" prop="name">
                <el-input v-model="form.name" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="花型描述" prop="flower_pattern">
                <el-input v-model="form.flower_pattern" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="上胶量g" prop="has_quantity">
                <el-input v-model="form.has_quantity" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="损耗率%" prop="loss_rate">
                <el-input v-model="form.loss_rate" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="门幅" prop="width">
                <el-input v-model="form.width" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="点辊用胶量" prop="use_quantity">
                <el-input v-model="form.use_quantity" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="可贴米数/KG" prop="meter_per_kg">
                <el-input v-model="form.meter_per_kg" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="是否启用" prop="status">
                <el-switch
                  v-model="form.status_id"
                  :active-value="0"
                  :inactive-value="-1"
                ></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="21">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-alert type="info" show-icon :closable="false">
                <p>1、1000÷点棍上胶量g/平方米÷点辊门幅/米=每公斤用胶可贴合的米数</p>
                <p>2、假设胶水选择25元/KG的， 25元÷64.6米=0.39元/米的胶水成本</p>
              </el-alert>
            </el-col>
          </el-row>
        </el-tab-pane>
      </sk-tabs>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";

export default {
  emits: ["success", "closed", "update:modeValue"],
  components: { skDicSelect },
  data() {
    return {
      mode: "add",
      unit: "All",
      titleMap: {
        add: "新增点棍",
        edit: "编辑点棍",
      },
      form: {
        status_id: 0,
        id: "",
        code: "",
        name: "",
        remark: "",
      },
      rules: {
        name: [{ required: true, message: "请输入代号" }],
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  watch: {
    "form.has_quantity"() {
      this.computequantity();
    },
    "form.loss_rate"() {
      this.computequantity();
    },
    "form.width"() {
      this.computequantity();
    },
  },
  methods: {
    computequantity() {
      if (
        this.$TOOL.isNumber(this.form.width) &&
        this.$TOOL.isNumber(this.form.has_quantity)
      ) {
        if (this.form.width > 0) {
          this.form.use_quantity = (this.form.has_quantity * this.form.width) / 1000;
        } else this.form.use_quantity = 0;

        if (this.form.has_quantity > 0 && this.$TOOL.isNumber(this.form.loss_rate)) {
          this.form.meter_per_kg = (
            (1000 / this.form.width / this.form.has_quantity) *
            (1 - this.form.loss_rate / 100)
          ).toFixed(2);
        } else this.form.meter_per_kg = 0;
      } else {
        this.form.use_quantity = 0;
        this.form.meter_per_kg = 0;
      }
    },
    //显示
    close() {
      this.visible = false;
      this.$emit("update:modeValue", false);
      this.$emit("closed");
    },
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.basedata.PointStick.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data);
    },
  },
};
</script>

<style></style>
